import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

import 'package:hzy_project_template/commpontents/commpontents_index.dart';
import 'package:hzy_project_template/config/config_index.dart';
import 'package:hzy_project_template/utils/utils_index.dart';

class LoginAgreementWidget extends StatefulWidget {
  const LoginAgreementWidget({
    super.key,
    required this.tapBoolCallback,
  });

  final TapBoolCallback tapBoolCallback;
  @override
  State<StatefulWidget> createState() {
    return LoginAgreementState();
  }
}

class LoginAgreementState extends CommonStateWidget<LoginAgreementWidget> {
  bool isAgree = false;
  @override
  Widget createBody({
    required BuildContext context,
    BoxConstraints? constraints,
  }) {
    Widget body = Row(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        createAgreeBtnWidget(),
        createAgreementRichWidget(),
      ],
    );
    return body;
  }

  /// 构建勾选按钮
  Widget createAgreeBtnWidget() {
    Widget body = HzyBtnWidget(
      btnLayoutType: BtnLayoutType.img,
      imageWidget: configNormalCheckBoxWidget(
        color: isAgree ? loginBlackColor : HzyNormalColorS.cola6a6a6,
        size: 22,
        isSelcet: isAgree,
      ),
      padding: EdgeInsets.only(
        right: 8.w,
      ),
      tapCall: () {
        configSetState(() {
          isAgree = !isAgree;
          widget.tapBoolCallback(isAgree);
        });
      },
    );

    return body;
  }

  /// 构建协议富文本
  Widget createAgreementRichWidget() {
    Widget body = RichText(
      text: TextSpan(
        children: [
          TextSpan(
            text: NoteLauncIdConfig.accountBeginMsg.tr,
            style: HzyTextStyle.fontMedium(
              size: Dimens.font_14,
              color: loginNoAgreementColor,
            ),
          ),
          TextSpan(
            text: "《${NoteLauncIdConfig.serviceAgreement.tr}》",
            style: HzyTextStyle.fontMedium(
              size: Dimens.font_14,
              color: loginAgreementColor,
            ),
            recognizer: TapGestureRecognizer()
              ..onTap = () => tapAgreementWithMethod(
                    type: 0,
                  ),
          ),
          TextSpan(
            text: LaunchIdConfig.and.tr,
            style: HzyTextStyle.fontMedium(
              size: Dimens.font_14,
              color: loginNoAgreementColor,
            ),
          ),
          TextSpan(
            text: "《${NoteLauncIdConfig.privacyPolicy.tr}》",
            style: HzyTextStyle.fontMedium(
              size: Dimens.font_14,
              color: loginAgreementColor,
            ),
            recognizer: TapGestureRecognizer()
              ..onTap = () => tapAgreementWithMethod(
                    type: 1,
                  ),
          ),
        ],
      ),
    );
    body = Expanded(
      child: body,
    );
    return body;
  }

  /// 配置点击用户
  configIsTapAgreement({
    required bool yorn,
  }) {
    configSetState(() {
      isAgree = yorn;
    });
  }

  /// 点击用户协议
  tapAgreementWithMethod({
    required int type,
  }) {
    // currentToPage("name");
  }
}
